package com.gzsxy.NIO;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author xiaolong
 * @version 1.0
 * @description: 通过transferFrom拷贝文件
 * @date 2022/4/23 19:58
 */
public class NIOFileChannel04 {

    public static void main(String[] args) throws Exception {

        FileInputStream fileInputStream = new FileInputStream("1.txt");
        FileChannel sourceCh = fileInputStream.getChannel();

        FileOutputStream fileOutputStream1 = new FileOutputStream("2.txt");
        FileChannel destCh = fileOutputStream1.getChannel();

        //使用transferFrom完成拷贝，从目标管道中复制数据到当前管道
        destCh.transferFrom(sourceCh,0,sourceCh.size());
        //关闭相关通道和流
        sourceCh.close();
        destCh.close();
        fileOutputStream1.close();
        fileInputStream.close();

    }
}
